/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop.aspectj;

import org.springframework.core.Ordered;
import org.springframework.lang.Nullable;

/**
 * Interface implemented to provide an instance of an AspectJ aspect.
 * Decouples from Spring's bean factory.
 *
 * <p>Extends the {@link org.springframework.core.Ordered} interface
 * to express an order value for the underlying aspect in a chain.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @since 2.0
 * @see org.springframework.beans.factory.BeanFactory#getBean
 */
/*
1. 总结
AspectInstanceFactory 是一个接口，用于提供 AspectJ 切面的实例，解耦了切面实例创建与 Spring 的 bean 工厂
	该接口继承了 Ordered 接口，允许为切面链中的底层切面表达顺序值
	它是 Spring AOP 处理 AspectJ 切面的核心基础设施接口之一
2. 内部成员说明
	Object getAspectInstance()：创建并返回此工厂的切面实例，返回值永不为 null
	ClassLoader getAspectClassLoader()：暴露此工厂使用的切面类加载器，如果使用引导类加载器则返回 null
3. 类关系结构
 AspectInstanceFactory (interface)	核心接口，用于提供 AspectJ 切面的实例
    ├── MetadataAwareAspectInstanceFactory (interface)	（专门用于处理 @AspectJ注解类）AspectInstanceFactory 的子接口，增加了获取切面元数据的能力
    │   ├── BeanFactoryAspectInstanceFactory (class)	使用 Spring 的 BeanFactory 来获取切面实例
    │   │   └── PrototypeAspectInstanceFactory (class)	处理原型作用域切面的实例工厂实现（每次调用 getAspectInstance() 都会创建新的切面实例）
    │   ├── LazySingletonAspectInstanceFactoryDecorator (class)	懒加载单例切面实例工厂装饰器(延迟初始化切面实例，只有在第一次需要时才创建)
    │   ├── SingletonMetadataAwareAspectInstanceFactory (class)	单例模式的元数据感知切面实例工厂(确保切面实例在整个应用生命周期中只创建一次)
    │   └── SimpleMetadataAwareAspectInstanceFactory (class)	简单的元数据感知切面实例工厂(提供基本的元数据访问功能，不依赖 Spring 的 BeanFactory)
    ├── SingletonAspectInstanceFactory (class)	单例切面实例工厂(确保切面实例在整个应用生命周期中只创建一次)
    │   └── SingletonMetadataAwareAspectInstanceFactory (class)	单例模式的元数据感知切面实例工厂(确保切面实例在整个应用生命周期中只创建一次)
    ├── SimpleBeanFactoryAwareAspectInstanceFactory (class)	简单的 BeanFactory 感知切面实例工厂(简单地包装了 BeanFactory 的功能，不提供额外的元数据支持)
    └── SimpleAspectInstanceFactory (class)	简单的切面实例工厂(确保切面实例在整个应用生命周期中只创建一次)


相关类和接口区别
	AspectInstanceFactory：核心接口，用于提供 AspectJ 切面的实例
	MetadataAwareAspectInstanceFactory：AspectInstanceFactory 的子接口，增加了获取切面元数据的能力
	BeanFactoryAspectInstanceFactory：基于 Spring BeanFactory 的切面实例工厂实现
	PrototypeAspectInstanceFactory：处理原型作用域切面的实例工厂实现
	LazySingletonAspectInstanceFactoryDecorator：懒加载单例切面实例工厂装饰器
	SingletonMetadataAwareAspectInstanceFactory：单例模式的元数据感知切面实例工厂
	SimpleMetadataAwareAspectInstanceFactory：简单的元数据感知切面实例工厂
	SingletonAspectInstanceFactory：单例切面实例工厂
	SimpleBeanFactoryAwareAspectInstanceFactory：简单的 BeanFactory 感知切面实例工厂
	SimpleAspectInstanceFactory：简单的切面实例工厂
4. 使用场景
	在 Spring AOP 中创建和管理 AspectJ 切面实例
	解耦切面实例化过程与具体的 Spring Bean 工厂实现
	支持不同作用域（单例、原型等）的切面实例化需求
	提供切面类加载器信息，确保类加载的一致性
5. 实际代码应用示例
// 在 AOP 配置中使用 AspectInstanceFactory
public class AopConfig {

    public AspectInstanceFactory createAspectFactory(Object aspectInstance) {
        return new AspectInstanceFactory() {
            @Override
            public Object getAspectInstance() {
                return aspectInstance;
            }

            @Override
            public ClassLoader getAspectClassLoader() {
                return aspectInstance.getClass().getClassLoader();
            }

            @Override
            public int getOrder() {
                return Ordered.LOWEST_PRECEDENCE;
            }
        };
    }
}

// ReflectiveAspectJAdvisorFactory 中使用示例
public class ReflectiveAspectJAdvisorFactory {
    public Advisor getAdvisor(Method candidateAdviceMethod,
                              MetadataAwareAspectInstanceFactory aspectInstanceFactory,
                              int declarationOrder, String aspectName) {
        // 使用 aspectInstanceFactory 获取切面实例
        Object aspectInstance = aspectInstanceFactory.getAspectInstance();
        // ... 其他处理逻辑
        return advisor;
    }
}

 */
public interface AspectInstanceFactory extends Ordered {

	/**
	 * Create an instance of this factory's aspect.
	 * @return the aspect instance (never {@code null})
	 */
	Object getAspectInstance();

	/**
	 * Expose the aspect class loader that this factory uses.
	 * @return the aspect class loader (or {@code null} for the bootstrap loader)
	 * @see org.springframework.util.ClassUtils#getDefaultClassLoader()
	 */
	@Nullable
	ClassLoader getAspectClassLoader();

}
